{*
  Object Pascal simple Developer Express manipulation routines
  @Author  Maciej Zagozda (1998-2008)
  @Licence Microsoft Reciprocal License (Ms-RL)
  @Website http://www.codeplex.com/intelitechdelphirtns/
  @Version 2008/04/20 Added documentation support
}

unit DevExpressRoutines;

interface

uses
  SysUtils,
  Classes,
  Contnrs,
  Dialogs,

  cxGrid,
  cxImageComboBox,
  cxListBox,
  cxLookupGrid,
  cxGridTableView, cxGridCustomView, cxGridCustomTableView,
  cxCustomData,
  cxControls,
  cxContainer;

function GetImageComboBoxItemIndex(const theControl: TcxImageComboBox; strEditValue: AnsiString): Longint;
  
procedure InitialiseControllerBeforeData(var AController: TcxCustomDataController; ARecordCount: integer);
procedure FinaliseControllerAfterData(var AController: TcxCustomDataController; iFocusedOld: integer);
function GetGridColumnIndex(const aView: TcxGridTableView; const aColumn: TcxGridColumn): Integer;
function SetGridColumnSelectAll(var aController: TcxCustomDataController; const aView: TcxGridTableView; const aColumn: TcxGridColumn; const aSelectAll: Boolean ): Boolean;
function SelectGridRow(aGrid: TcxGrid; aView: TcxCustomGridView; intIndex: Integer): Boolean;


implementation

{*
Retrieves image combo box item index by combo box item name
@param theControl - TcxImageComboBox object
@param strEditValue - edit value to search for
*}

function GetImageComboBoxItemIndex(const theControl: TcxImageComboBox; strEditValue: AnsiString): Longint;
var
  intA: Longint;
  strItemValue: AnsiString;
begin

  Result := -1;
  strEditValue := AnsiUpperCase(strEditValue);
  with TcxImageComboBoxProperties(theControl.Properties) do
  begin
    for intA := 0 to Items.Count -1 do
    begin
      strItemValue := AnsiUpperCase(Items[intA].Value);
      //ShowMessage('GetImageComboBoxItemIndex: strItemValue=' + strItemValue );

      if AnsiCompareText(strItemValue, strEditValue) =0 then
      begin
        Result := intA;
        Break;
      end;//if
    end;//for
  end;//with
end;

{*
  Gets the index of a column within a grid
  @param aGrid - Grid object containing the column
  @param aColumn - Column object
  @return Index of a column with the grid
*}

function GetGridColumnIndex(const aView: TcxGridTableView; const aColumn: TcxGridColumn): Integer;
begin
  Result := aView.IndexOfItem(aColumn);
end;

{*
Selects all records in the controller by putting or clearing check mark in aColumn belonging to alView
*}

function SetGridColumnSelectAll(var aController: TcxCustomDataController;
                                const aView: TcxGridTableView;
                                const aColumn: TcxGridColumn;
                                const aSelectAll: Boolean ): Boolean;
var
  intA: Integer;
begin

  if not Assigned(aController) then raise Exception.Create('Function SetGridColumnSelectAll requires aController to be assigned');
  if not Assigned(aView) then raise Exception.Create('Function SetGridColumnSelectAll requires aView to be assigned');
  if not Assigned(aColumn) then raise Exception.Create('Function SetGridColumnSelectAll requires aColumn to be assigned');

  if Assigned(aController) and Assigned(aView) and Assigned(aColumn) then
  begin
    aView.BeginUpdate;
    with aController do
    begin
      for intA := 0 to aController.RecordCount -1 do
      begin
        Values[intA,GetGridColumnIndex(aView,aColumn)] := aSelectAll;
      end;//for
    end;//with
    aView.EndUpdate;
    Result := True;
  end
  else
  begin
    Result := False;
  end;//if
end;

procedure InitialiseControllerBeforeData(var AController: TcxCustomDataController; ARecordCount: integer);
begin
  AController.BeginUpdate;
  while(AController.RecordCount > 0) do
  begin
    try
      AController.DeleteRecord(0);
    except
    end;
  end;
  AController.RecordCount := ARecordCount;
end;

procedure FinaliseControllerAfterData(var AController: TcxCustomDataController; iFocusedOld: integer);
begin
  AController.EndUpdate;
  if (iFocusedOld = -1) or (AController.RecordCount < iFocusedOld) then iFocusedOld := 0;
  AController.FocusedRowIndex := iFocusedOld;
end;

{*
  Selects a row in a grid. If the view is not specified, the default view is being used
  @param aGrid - the grid object containing view
  @param aView - the view object containing row
  @param intIndex - row index to highlight
  @return True if row has been found and selected, False otherwise
*}

function SelectGridRow(aGrid: TcxGrid; aView: TcxCustomGridView; intIndex: Integer): Boolean;
var
  theRow: Variant;
begin
 if not Assigned(aView) then aView := aGrid.ActiveView;
 try
   //theRow := aView.ViewData.
   //theRow.Selected := true;
 except
   Result := False;
 end;//try
end;



end.
